/*
 * @Author: 谢子健 1075010289@qq.com
 * @Date: 2024-09-17 13:29:25
 * @LastEditors: 谢子健 1075010289@qq.com
 * @LastEditTime: 2024-09-17 13:32:16
 * @FilePath: \ilink-vue-frontend\src\stores\useCourseStore.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// src/store/useCourseStore.ts
import { defineStore } from "pinia";

interface Course {
  courseId: string;
  teacherName: string;
  teacherId: string;
  courseTag: string;
  courseDescription: string;
  courseSchedule: string;
  courseName: string;
  courseImage: string | null;
  folderId: string;
  viewNum: string;
  createdAt: string;
  img: string;
  cardName: string;
  description: string;
  views: string;
  path: string;
}

interface CourseState {
  courses: Course[];
  total: number;
  currentPage: number;
}

export const useCourseStore = defineStore("courseStore", {
  state: (): CourseState => ({
    courses: [],
    total: 0,
    currentPage: 1,
  }),
  actions: {
    setCourses(courses: Course[], total: number, currentPage: number) {
      // eslint-disable-next-line arrow-parens
      const mappedCourses = courses.map((course) => ({
        ...course,
        img: course.courseImage || "/default-image.jpg", // 映射 courseImage 到 img，添加默认图片
        cardName: course.courseName, // 映射 courseName 到 cardName
        description: course.courseDescription, // 映射 courseDescription 到 description
        views: `${course.viewNum} 已学习`, // 格式化 views
        path: `/course/${course.courseId}`, // 派生路径
        folderId: course.folderId, // 包含 folderId
      }));

      this.courses = mappedCourses;
      this.total = total;
      this.currentPage = currentPage;
    },
  },
});
